#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    // 打开读文件流
    int r_fd = open("text.txt", O_RDONLY);
    if (r_fd == -1)
    {
        printf("打开读文件流失败\n");
        return -1;
    }

    // 打开写文件流
    int w_fd = open("text_copy.txt", O_WRONLY | O_CREAT | O_APPEND, 0644);
    if (w_fd == -1)
    {
        printf("打开写文件流失败\n");
        close(r_fd);
        return -1;
    }

    // 边读边写
    char buff[100] = {0};
    int read_len = 0;
    while ((read_len = read(r_fd, buff, 100)) > 0)
    {
        write(w_fd, buff, read_len);
        write(STDOUT_FILENO, buff, read_len);
    }

    // 关闭文件
    close(r_fd);
    close(w_fd);

    return 0;
}